package de.sp.games.snake12k;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class ScoreList
{
	private static ScoreList INSTANCE = new ScoreList();
	
	private ArrayList<Score> scoreList;
	private SaveSystem saveSystem = SaveSystem.getInstance();
	
	private ScoreList() 
	{
		initScoreList();
	}

	private void initScoreList()
	{
		scoreList = new ArrayList<Score>();
		/* Add empty/start scores*/
		scoreList.add(new Score("staxx6", 500, 0));
		scoreList.add(new Score("ThePixel5D", 500, 1));
		scoreList.add(new Score("Cpt.Waldo", 400, 2));
		scoreList.add(new Score("Plane", 300, 3));
		scoreList.add(new Score("Snake", 200, 4));
		scoreList.add(new Score("MISSINGO", 200, 5));
		
		loadScoreFile();
	}
	
	public void saveScoreFile()
	{
		System.out.println("Saveing");
		System.out.println("### Scorelist: ###");
		saveSystem.startOutputStream();
		for(int i = 0; i < 5; i++)
		{
			for(Score score: scoreList)
			{
				if(score.getIndex() == i)
				{
					System.out.println(score.getIndex() + " - " + score.getName() + " : " + score.getScoreNumber());
					saveSystem.writeScore(score);
				}
			}
		}
		saveSystem.closeOutputStream();
		System.out.println("##################");
	}
	
	private void loadScoreFile()
	{
		if(saveSystem.existFile())
		{
			scoreList.clear();
			saveSystem.startInputStream();
			for(int i = 0; i < 5; i++) {
				scoreList.add(saveSystem.loadScore());
			}
		}
	}
	
	public void makeNewEntry(final String name, final int scoreNumber)
	{
		if(name != null && scoreNumber >= 0)
		{
			int newIndex = findNewIndex(scoreNumber);
			decreaseOldScoreIndex(newIndex);
			scoreList.add(newIndex, new Score(name, scoreNumber, newIndex));
			
			saveScoreFile();
		} else
		{
			System.out.println("ERROR: Invalid score entry!");
		}
	}
	
	private int findNewIndex(final int scoreNumber)
	{
		int index = 5;
		
		if(scoreNumber >= 0)
		{
			for(Score score : scoreList)
			{
				if(index >= score.getIndex() && scoreNumber > score.getScoreNumber())
				{
					index = score.getIndex();
				}
			}
		}
		return index;
	}
	
	private void decreaseOldScoreIndex(final int index)
	{
		for(Score score: scoreList)
		{
			// Push index one down
			if(score.getIndex() >= index)
			score.setIndex(score.getIndex() + 1);
		}
		// Remove the last
		for(int i = 0; i < scoreList.size(); i++)
		{
			if(scoreList.get(i).getIndex() == scoreList.size())
			{
				scoreList.remove(i);
			}
		}
	}
	
	public Score getScore(final int index)
	{
		if(index >= 0 && index <= 4)
		{
			for(Score score: scoreList)
			{
				if(index == score.getIndex())
				{
					return score;
				}
			}
		} else
		{
			new IllegalArgumentException();
		}
		return null;
	}
	
	public boolean isNewHighScore(final int scoreNumber) 
	{
		boolean isNewHighScore = false;
		
		if(scoreNumber > 0)
		{
			for(Score score: scoreList)
			{
				if(score.getScoreNumber() < scoreNumber)
				{
					isNewHighScore = true;
					break;
				}
			}
		}
		return isNewHighScore;
	}
	
	public static ScoreList getInstance() { return INSTANCE; }
}
